include constants.doh

/// Purpose: Generate the native sample results (mostly regional level measures)


////////////////////////////////////////
///////// Read in Natives Data /////////
////////////////////////////////////////

// See the README for instructions on how to access this data
import delimited "input/pipeline/native_regressions_input.csv", clear

replace curr_nuts3 = "_dummy" if curr_nuts3 == "`baseline_region'"
encode curr_nuts3, gen(nuts3_encoded)
fvset base last nuts3_encoded

// Make the total friend/group/cont produced controls
gen ln_other_frnd = log(total_30d_other_country_frnds + 1)
gen ln_other_grps = log(other_grps + 1)
gen ln_cont_prod = 1

winsor2 n_frnd_nat_lcl, cuts(0 99) replace
winsor2 n_lcl_50n_grps, cuts(0 99) replace

////////////////////////////////////////////////////////////
/// Use the regional FEs to try to predict demographics ///
////////////////////////////////////////////////////////////

eststo clear

eststo panel1_age: reghdfe age, absorb(i.nuts3_encoded) tol(1e-5)
estadd ysumm
eststo panel2_female: reghdfe is_female, absorb(i.nuts3_encoded) tol(1e-5)
estadd ysumm
eststo panel3_college: reghdfe has_college, absorb(i.nuts3_encoded) tol(1e-5)
estadd ysumm
eststo panel4_total_frnds: reghdfe total_30d_frnds, absorb(i.nuts3_encoded) tol(1e-5)
estadd ysumm
eststo panel5_n_frnd_nat_lcl: reghdfe n_frnd_nat_lcl, absorb(i.nuts3_encoded) tol(1e-5)
estadd ysumm
eststo panel6_indiv_time_cntrl: reghdfe indiv_time_cntrl, absorb(i.nuts3_encoded) tol(1e-5)
estadd ysumm
eststo panel7_indiv_l90_cntrl: reghdfe indiv_l90_cntrl, absorb(i.nuts3_encoded) tol(1e-5)
estadd ysumm
eststo panel8_indiv_l1080_cntrl: reghdfe indiv_l1080_cntrl, absorb(i.nuts3_encoded) tol(1e-5)
estadd ysumm

custom_xml_tab "output" "nuts3_fe_rsq_native"

eststo clear

keep n_frnd_nat_lcl n_lcl_50n_grps nuts3_encoded age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female prev_qs_nuts3 indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl ln_other_frnd ln_other_grps ln_cont_prod

compress

///////// First just the averages, Ns, and variances outside of a regression framework //////////

preserve

    collapse (mean) n_frnd_nat_lcl n_lcl_50n_grps (count) n=n_frnd_nat_lcl, by(nuts3_encoded)

    export delimited "output/natives_nuts3_avgs_and_Ns.csv"

restore

preserve

    collapse (sd) n_frnd_nat_lcl n_lcl_50n_grps, by(nuts3_encoded)

    export delimited "output/natives_nuts3_sds.csv"

restore

//////// For appendix, regression coefficients ///////////

eststo clear

eststo panel1: reghdfe n_frnd_nat_lcl age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl ln_other_frnd ln_other_grps ln_cont_prod, absorb(i.prev_qs_nuts3 i.nuts3_encoded) tol(1e-5)
estadd ysumm
eststo panel2: reghdfe n_lcl_50n_grps age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl ln_other_frnd ln_other_grps ln_cont_prod, absorb(i.prev_qs_nuts3 i.nuts3_encoded) tol(1e-5)
estadd ysumm

custom_xml_tab "output" "deters_natives"

eststo clear



///////////////////////////////////////////////////////////////////
///////// Natives: Friending to Migrants / Proasyl Groups /////////
///////////////////////////////////////////////////////////////////

// See the README for instructions on how to access this data
import delimited "input/pipeline/native_regressions_proasyl_input.csv", clear

replace curr_nuts3 = "_dummy" if curr_nuts3 == "`baseline_region'"
encode curr_nuts3, gen(nuts3_encoded)
fvset base last nuts3_encoded

winsor2 n_frnd_sy_lcl, cuts(0 99) replace
winsor2 n_frnd_nat_lcl, cuts(0 99) replace

gen rel_frnd = (n_frnd_sy_lcl/n_frnd_nat_lcl) / (nuts3_sy/nuts3_native)

eststo clear

eststo panel1: reghdfe n_frnd_sy_lcl age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female has_college indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl, absorb(i.prev_qs_nuts3) tol(1e-5)
estadd ysumm
eststo panel2: reghdfe n_frnd_sy_lcl age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female has_college indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl, absorb(i.prev_qs_nuts3 curr_nuts3) tol(1e-5)
estadd ysumm
eststo panel3: reghdfe n_frnd_nat_lcl age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female has_college indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl, absorb(i.prev_qs_nuts3) tol(1e-5)
estadd ysumm
eststo panel4: reghdfe n_frnd_nat_lcl age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female has_college indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl, absorb(i.prev_qs_nuts3 curr_nuts3) tol(1e-5)
estadd ysumm
eststo panel5: reghdfe rel_frnd age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female has_college indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl, absorb(i.prev_qs_nuts3) tol(1e-5)
estadd ysumm
eststo panel6: reghdfe rel_frnd age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female has_college indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl, absorb(i.prev_qs_nuts3 curr_nuts3) tol(1e-5)
estadd ysumm
eststo panel7: reghdfe in_proasyl age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female has_college indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl, absorb(i.prev_qs_nuts3 i.total_grps) tol(1e-5)
estadd ysumm
eststo panel8: reghdfe in_proasyl age_brkt_25_34 age_brkt_35_44 age_brkt_45_54 age_brkt_55 is_female has_college indiv_time_cntrl indiv_l90_cntrl indiv_l1080_cntrl, absorb(i.prev_qs_nuts3 i.total_grps curr_nuts3) tol(1e-5)
estadd ysumm

custom_xml_tab "output/deters_natives_to_non_natives"

eststo clear


///////// First just the averages, Ns, and variances outside of a regression framework //////////

preserve

    collapse (mean) in_proasyl in_admin_proasyl in_top5_proasyl (count) n=in_proasyl, by(nuts3_encoded)

    export delimited "output/natives_proasyl_nuts3_avgs_and_Ns.csv"

restore

preserve

    collapse (sd) in_proasyl in_admin_proasyl in_top5_proasyl, by(nuts3_encoded)

    export delimited "output/natives_proasyl_nuts3_sds.csv"

restore
